package com.wedo.mall.activity.user;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.wedo.mall.R;
import com.wedo.mall.entity.RechargeAmount;

import butterknife.BindView;
import butterknife.OnClick;

public class RechargeAmountActivity extends EActivity {

    @BindView(R.id.iv_rechargeamount_bg) ImageView iv_rechargeamount_bg;
    @BindView(R.id.tv_lastearning) TextView tv_lastearning;
    @BindView(R.id.tv_allearning) TextView tv_allearning;
    @BindView(R.id.tv_valueaddedratio) TextView tv_valueaddedratio;

    @Override
    protected int layout() {
        return R.layout.activity_recharge_amount;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        fixMyWalletBGHeight();
        getRechargeAmount();
    }

    /**
     * 动态适配头部背景图片高度
     */
    private void fixMyWalletBGHeight(){
        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mywallet_bg, options);
        //获取bitmap的宽度
        float bitWidth = bitmap.getWidth();
        //获取bitmap的宽度
        float bitheight = bitmap.getHeight();
        //计算出图片的高宽比，然后按照图片的比列去缩放图片
        float bitScale =  bitheight / bitWidth;
        //获取屏幕宽度
        WindowManager wm = this.getWindowManager();
        int screenWidth = wm.getDefaultDisplay().getWidth();

        ViewGroup.LayoutParams  lp = iv_rechargeamount_bg.getLayoutParams();
        //设置imageView宽
        lp.height =  (int)(screenWidth * bitScale);
        iv_rechargeamount_bg.setLayoutParams(lp);

    }

    /**
     * 获取个人信息
     */
    private void getRechargeAmount(){
        loading();
        EClient.with().post(EApi.USER_RECHARGEAMOUNT, new EObserver() {
            @Override
            public void successful(EResponse response) {

                RechargeAmount rechargeAmount = response.object("list",RechargeAmount.class);
                tv_lastearning.setText(rechargeAmount.last_earning);
                tv_allearning.setText(rechargeAmount.all_earning);
                tv_valueaddedratio.setText(rechargeAmount.valueAddedRatio);
                dismiss();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }

        });
    }

    @OnClick(R.id.iv_leftarrow)
    protected void onPortraitClick(){
        finish();
    }

    @OnClick(R.id.rl_recharge)
    protected void onRechargeClick(){
        toActivity(MyWalletRechangeActivity.class);
    }
}
